package 代码记录.指定公司代码练习记录.西安.分值100分.自己肯定做的出来;

import java.util.Scanner;

/**
 * @author zx
 * @create 2023-07-08 23:33
 * 我的做法
 */
public class IPv4地址转换成整数 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] temp = str.split("#");
        int[] arr = new int[4];
        for (int i = 0; i < temp.length; i++) {
            try {
                arr[i] = Integer.parseInt(temp[i]);
            }catch (NumberFormatException e){
                System.out.println("invalid IP\n");//空串、非法字符都会
                return;
            }
            if(i == 0 && (arr[i] < 1 || arr[i] > 128)){
                System.out.println("invalid IP\n");//不符合给定IP范围
                return;
            }else if(temp.length != 4){
                System.out.println("invalid IP\n");//非法IP
                return;
            }else if(i >= 1 && (arr[i] < 0 || arr[i] > 255)){
                System.out.println("invalid IP\n");//非法#分十进制
                return;
            }
        }
        String[] num = new String[4];
        String res = "";
        for(int i = 0;i < arr.length;i++){
            num[i] = Integer.toString(arr[i],2);
            while(num[i].length() < 8){
                num[i] = "0" + num[i];
            }
            res = res + num[i];
        }
        System.out.println(Integer.valueOf(res,2));
    }
}
